package com.xian.demo;

import java.util.Random;
import java.util.Scanner;

public class GuessNum {
    public static void main(String[] args) {
        //猜数字:1-20随机数，用户猜，猜中打印第一次出现的索引和内容,没有猜中继续

        //1.定义一个数组
        int[] datas = new int[5];

        //2.随机生成20个存入数组
        Random r = new Random();
        for (int i = 0; i < datas.length; i++) {
            datas[i] = r.nextInt(20) + 1;
        }

        //3.用户输入
        Scanner sc = new Scanner(System.in);
        OUT:
        while (true) {
            System.out.println("请输入1-20数字:");
            int getNum = sc.nextInt();
            //4.比较
            for (int i = 0; i < datas.length; i++) {
                if (datas[i] == getNum) {
                    System.out.println("猜中啦！！！");
                    System.out.println("猜中第" + (i + 1 )+ "个" + "值为:" + datas[i]);
                    break OUT; //结束了整个循环
                }

            }
            System.out.println("没有猜中哦！！！继续猜吧！！！");
        }

        //5.输出全部元素，让用户看到
        for (int i = 0; i < datas.length; i++) {
            System.out.print(datas[i] + "\t");

        }

    }
}
